namespace net.podhola.gtd.controllers

import System
import System.Configuration
import System.Globalization
import System.Web
import System.Web.UI
import System.Web.UI.WebControls
import Castle.MonoRail.Framework
import net.podhola.gtd.finances

[Layout("default"),Scaffolding( Account )]
class AccountsController(GTDParentController):
	
	public override def Title():		
		return "Accounts"

	override def GetWelcomeNumber() as string:
		return Account.FindAll().Length.ToString()
		
	override def GetWelcomeText() as string:
		return "accounts"

	override def GetWelcomeUrl() as string:
		return "/accounts/list.rails"

	def FindAccount( id as int ) as Account:
		PropertyBag["Account"] = Account.Find( id )
		return PropertyBag["Account"]

	public def Detail( id as int ):
		FindAccount( id )

	public def Edit( id as int ):
		FindAccount( id )
		
	public def Update( [DataBind("Account")] Account as Account ):
		Account.Save()
		RedirectToAction("Detail", { "id":Account.Id } )

	public def New():
		pass
		
	public def Create( Name, Amount, InitDate ):
		account = Account()
		account.Name = Name		
		account.Create()
		amount as double = Double.Parse( Amount, NumberStyles.Currency )
		asOf as DateTime  = DateTime.Parse( InitDate)			
		account.PrepareInitialBalance( asOf, amount ).Save()

		PropertyBag["Account"] = account
		
	public def Delete( id as int ):
		FindAccount( id ).Delete()
		RedirectToAction("Index" )

	public def List():
		pass

	public def BalanceToActualValue( id as int ):
		FindAccount( id )

	def SaveActualValue( amount as double ):
		accountId = Int32.Parse( Form["Account.Id"] )
		FindAccount( accountId ).PrepareActualBalance( amount ).Save()
		RedirectToAction("Detail", { "id":accountId } )
